package pers.cz.job;

import pers.cz.commons.datasource.DbKeyHolder;
import pers.cz.mapper.ImsJsonSoap0Mapper;
import pers.cz.model.ImsJsonSoap0;
import pers.cz.model.ImsJsonSoap0Example;
import pers.cz.quartz.job.AbstractQuartzJob;
import pers.cz.http.HttpUtils;
import pers.cz.spring.SpringContextUtil;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 测试将常规环境接口重新请求一次服务，以此来截获出入参报文，不推荐使用
 * @program: PostGirl-panent
 * @description: InterfaceRecordJob
 * @author: Cheng Zhi
 * @create: 2021-12-28 17:46
 **/
public class InterfaceRecordJob extends AbstractQuartzJob {

    @Override
    public void doJob(JobExecutionContext var1, Map params) throws JobExecutionException {
        this.work();
    }

    private void work(){

        ThreadPoolExecutor exePool = new ThreadPoolExecutor(10, 10, 0L,
                TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(100));
        exePool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

        // 查询常规数据库
        ImsJsonSoap0Example example = new ImsJsonSoap0Example();
        ImsJsonSoap0Example.Criteria criteria = example.createCriteria();
        criteria.andExt4IsNotNull();
        DbKeyHolder.setDbKey("dev");
        List<ImsJsonSoap0> imsJsonSoap0s = SpringContextUtil.getBean(ImsJsonSoap0Mapper.class).selectAllAndExt4IsNotNull();
        //DbKeyHolder.setDbKey("master");
        // 组织参数
        Map<Integer, List<ImsJsonSoap0>> map = new HashMap<Integer, List<ImsJsonSoap0>>();
        for (ImsJsonSoap0 imsJsonSoap0 : imsJsonSoap0s) {
            int threadId = imsJsonSoap0.getJsonId().hashCode()%10;

            if (map.keySet().contains(threadId)) {
                map.get(threadId).add(imsJsonSoap0);
            } else {
                List<ImsJsonSoap0> list = new ArrayList<>();
                list.add(imsJsonSoap0);
                map.put(threadId, list);
            }
        }

        // 多线程处理
        for (Integer threadId : map.keySet()) {
            DealThread dealThread = new DealThread(threadId, map.get(threadId));
            exePool.execute(dealThread);
        }

    }

    class DealThread implements Runnable {

        private int threadId;
        private List<ImsJsonSoap0> imsJsonSoap0s;

        public DealThread(int threadId, List<ImsJsonSoap0> imsJsonSoap0s) {
            this.threadId = threadId;
            this.imsJsonSoap0s = imsJsonSoap0s;
        }

        @Override
        public void run() {

            for (ImsJsonSoap0 imsJsonSoap0 : imsJsonSoap0s) {

                // 这里发送请求，
                String request = imsJsonSoap0.getInputSoap();

                // 这里写死一个请求地址。
                String url = "http://10.9.237.63:19006/com.cmcc.sh.boss.interfaces.forint";
                String result = HttpUtils.getResultByPost(request, url);

                System.out.println(result);

                // 批量修改数据库库


            }

        }
    }
}
